﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MShop.Core.Domain
{
    public class SkuEntity
    {
        public SkuEntity(string skuId)
        {            
            this.SkuId = skuId;
        }

        public bool IsEmpty
        {
            get
            {
                return string.IsNullOrEmpty(SkuId);
            }
        }

        public string SkuId { get; set; }

        public Dictionary<string, string> ToDictionary()
        {
            Dictionary<string, string> dicts = new Dictionary<string, string>();
            if (!IsEmpty)
            {
                string[] str = SkuId.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in str)
                {
                    string[] arr = s.Split(new char[]{':'},StringSplitOptions.RemoveEmptyEntries);
                    dicts.Add(arr[0], arr[1]);
                }
            }
            return dicts;
        }

        public override int GetHashCode()
        {
            if (!IsEmpty)
            {
                return this.SkuId.GetHashCode();
            }
            return base.GetHashCode();
           
        }

        public override bool Equals(object obj)
        {
            
                if (obj == null)
                    return false;
                if (!IsEmpty)
                {
                    if (obj is SkuEntity)
                    {
                        SkuEntity m = (SkuEntity)obj;
                        return CompareEquals(m);
                    }

                }
                return false;
                            
        }

        private bool CompareEquals(SkuEntity sku)
        {
            foreach (KeyValuePair<string, string> item in ToDictionary())
            {
                if (!sku.ToDictionary().Contains(item))
                {
                    return false;
                }
            }
            return true;
        }

        public override string ToString()
        {
            if (!IsEmpty)
            {
                return this.SkuId.ToString();
            }
            return base.ToString();
        }

        public static bool operator ==(SkuEntity a, SkuEntity b)
       {
           return a.CompareEquals(b);
       }
        public static bool operator !=(SkuEntity a, SkuEntity b)
       {
           return !a.CompareEquals(b);
       }

        public static implicit operator string(SkuEntity f)
       {
           return f.SkuId;
       }

        public static implicit operator SkuEntity(string skuid)
       {
           return new SkuEntity(skuid);
       }

    }
}